
//  SRSplashViewController.m
//  SportRing
//
//  Created by shaohua on 14-9-14.
//  Copyright (c) 2014年 Weizhi. All rights reserved.
//

#import "SRSplashViewController.h"
#import "SRSplashView.h"
#import "SRSystemNetService.h"
#import "SRDB.h"
#import "Utils.h"
#import "SRManager.h"
#import "SRMainViewController.h"

@interface SRSplashViewController ()<UIAlertViewDelegate>

@property (nonatomic, weak) IBOutlet SRSplashView * splashView;
@property (nonatomic, weak) NSTimer * timer;
@property (nonatomic, strong)NSString * updateUrl;

@end

@implementation SRSplashViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    SRSystem * system = [SRSystem findDefaultSystem];
    SRSplashViewModel * viewModel = [[SRSplashViewModel alloc] init];
    viewModel.imageUrl = system.posterUrl;
    self.splashView.viewModel = viewModel;
    
    //    @weakify(self);
    
    [[SRSystemNetService checkVersionUpdate] subscribeNext:^(id x) {
        
        NSDictionary * dict = x;
        NSString * buildVersion = [Utils buildVersion];
        
        if ([dict[@"version"] compare:buildVersion options:NSNumericSearch] == NSOrderedDescending) {
            
            _updateUrl = dict[@"installUrl"];
            
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"更新提示"
                                                                message:@"有新的版本可以更新啦"
                                                               delegate:self
                                                      cancelButtonTitle:@"取消"
                                                      otherButtonTitles:@"确定", nil];
            
            [alertView show];
            
        }
    } error:^(NSError *error) {
        
        //        [self_weak_ goLogin];
    }];
    
    [SRSystemNetService getPoster];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self isLogin];
        //        [self_weak_.splashView dismissSplashWithBlock:^{
        //            [self_weak_ performSegueWithIdentifier:@"modalToLogin" sender:self];
        //        }];
        
        
        
    });
}



- (void)viewDidAppear:(BOOL)animated
{
    static int i=0;
    i++;
    if (i == 1) {
        return;
    }
    [self isLogin];
}

- (void)isLogin
{
    if ([Utils userIsLogin])
    {
        [self goMain];
    }else
        [self goLogin];
}

//如果用户已经登录，直接跳转到主界面
- (void)goMain
{
    SRMe *me = [SRMe findUserAndCreateIfNotExistByName:[Utils loginedUserName]];
    
    [SRMe setCurrentUser:me];
    
    SRDevice * device = [SRDevice findDeviceForMe:[SRMe currentUser]];
    
    [[SRManager sharedInstance] setCurrentPeripheralName:device.name];
    
    [self performSegueWithIdentifier:@"gotoMainVC" sender:self];
    
    [[SRManager sharedInstance] readyToStartUp];
    if (!self.splashView.hidden) {
        [UIView animateWithDuration:1 animations:^{
            self.splashView.alpha = 0;
        }];
    }
    
}

-(void)goLogin
{
//    [NSThread sleepForTimeInterval:10];
    [self performSegueWithIdentifier:@"modalToLogin" sender:self];
    
    if (!self.splashView.hidden) {
        [UIView animateWithDuration:1 animations:^{
            self.splashView.alpha = 0;
        }];
    }
    
    
    //    @weakify(self);
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //
    ////        [self_weak_.splashView dismissSplashWithBlock:^{
    ////            [self_weak_ performSegueWithIdentifier:@"modalToLogin" sender:self];
    ////        }];
    //    });
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        NSString* installUrl = [NSString stringWithFormat:@"itms-services://?action=download-manifest&url=%@",_updateUrl];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:installUrl]];
    }
}

@end
